package cn.edu.scau.cmi.yewangjiang.spring;

import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;

import cn.edu.scau.cmi.yewangjiang.hibernate.dao.StudentDAO;
import cn.edu.scau.cmi.yewangjiang.hibernate.dao.TeacherDAO;
import cn.edu.scau.cmi.yewangjiang.hibernate.domain.Student;
import cn.edu.scau.cmi.yewangjiang.hibernate.domain.StudentModel;
import cn.edu.scau.cmi.yewangjiang.hibernate.domain.Teacher;
import cn.edu.scau.cmi.yewangjiang.hibernate.domain.TeacherModel;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;

public class SpringController implements Initializable {
    @FXML
    private TableView studentTable, teacherTable;
    @FXML
    private TableColumn sid, sname, stutor, tid, tname;
    @FXML
    private TextField studentName, teacherName;
    @FXML
    private TextField studentNewName, teacherNewName;
    @FXML
    private ComboBox studentSelect, teacherSelect;
    @FXML
    private Button add, delete, update;
    @FXML
    private TextArea output;
    private StudentDAO sdao = new StudentDAO();
    private TeacherDAO tdao = new TeacherDAO();

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        studentSelect.setItems(FXCollections.observableArrayList());
        teacherSelect.setItems(FXCollections.observableArrayList());
        studentTable.setItems(FXCollections.observableArrayList());
        teacherTable.setItems(FXCollections.observableArrayList());
        sid.setCellValueFactory(new PropertyValueFactory<>("id"));
        sname.setCellValueFactory(new PropertyValueFactory<>("name"));
        stutor.setCellValueFactory(new PropertyValueFactory<>("teacher"));
        tid.setCellValueFactory(new PropertyValueFactory<>("id"));
        tname.setCellValueFactory(new PropertyValueFactory<>("name"));
        updateTable();
    }

    public void add() {
        String studentN = studentName.getText();
        String teacherN = teacherName.getText();
        if (studentN.compareTo("") != 0) {
            Student s = new Student();
            s.setName(studentN);
            if (sdao.save(s) == true) {
                output.appendText("新建学生 " + studentN + "\n");
                studentName.setText("");
            }
        }
        if (teacherN.compareTo("") != 0) {
            Teacher t = new Teacher();
            t.setName(teacherN);

            if (tdao.save(t) == true) {
                output.appendText("新建老师 " + teacherN + "\n");
                teacherName.setText("");
            }
        }
        updateTable();
    }

    public void delete() {
        String studentN = studentName.getText();
        String teacherN = teacherName.getText();
        if (studentN.compareTo("") != 0) {
            List tmp = sdao.findByName(studentN);

            if (tmp != null && tmp.size() != 0) {
                Student s = (Student) (tmp.toArray()[0]);

                if (sdao.delete(s) == true) {
                    output.appendText("删除学生 " + studentN + "\n");
                    studentName.setText("");
                }
            }
        }
        if (teacherN.compareTo("") != 0) {
            List tmp = tdao.findByName(teacherN);
            if (tmp != null && tmp.size() != 0) {
                Teacher t = (Teacher) (tmp.toArray()[0]);

                if (tdao.delete(t) == true) {
                    output.appendText("删除老师 " + teacherN + "\n");
                    teacherName.setText("");
                }
            }
        }
        updateTable();
    }

    public void update() {
        String studentN = studentName.getText();
        String teacherN = teacherName.getText();
        String studentNN = studentNewName.getText();
        String teacherNN = teacherNewName.getText();
        if (studentN.compareTo("") != 0) {
            List tmp = sdao.findByName(studentN);

            if (tmp != null && tmp.size() != 0) {
                Student s = (Student) (tmp.toArray()[0]);

                if (studentNN.compareTo("") != 0) {
                    s.setName(studentNN);
                    if (sdao.update(s) == true) {
                        output.appendText("学生 " + studentN + " 改名为  " + studentNN + "\n");
                        studentName.setText("");
                        studentNewName.setText("");
                    }
                }

                if (studentSelect.getValue() != null) {
                    tmp = tdao.findByName(studentSelect.getValue());
                    Teacher t = (Teacher) (tmp.toArray()[0]);

                    if (s.getTeacher() != null && s.getTeacher().getName().compareTo(t.getName()) == 0)
                        s.setTeacher(null);
                    else
                        s.setTeacher(t);

                    if (sdao.update(s) == true) {
                        output.appendText("学生修改导师 " + studentN + " 为 " + studentSelect.getValue() + "\n");
                        studentName.setText("");
                    }
                }
            }
        }
        if (teacherN.compareTo("") != 0) {
            List tmp = tdao.findByName(teacherN);
            if (tmp != null && tmp.size() != 0) {
                Teacher t = (Teacher) (tmp.toArray()[0]);
                if (teacherNN.compareTo("") != 0) {
                    t.setName(teacherNN);
                    if (tdao.update(t) == true) {
                        output.appendText("老师 " + teacherN + " 改名为 " + teacherNN + "\n");
                        teacherName.setText("");
                        teacherNewName.setText("");
                    }
                }
                if (teacherSelect.getValue() != null) {
                    tmp = sdao.findByName(teacherSelect.getValue());
                    Student s = (Student) (tmp.toArray()[0]);
                    if (s.getTeacher() != null && s.getTeacher().getName().compareTo(teacherN) == 0)
                        s.setTeacher(null);
                    else
                        s.setTeacher(t);
                    if (sdao.update(s) == true) {
                        output.appendText("老师修改学生 " + teacherN + " 为 " + teacherSelect.getValue() + "\n");
                        teacherName.setText("");
                    }
                }
            }
        }
        updateTable();
    }

    public void updateTable() {
        ObservableList<StudentModel> studentData = studentTable.getItems();
        ObservableList<TeacherModel> teacherData = teacherTable.getItems();
        ObservableList<String> studentSelData = studentSelect.getItems();
        ObservableList<String> teacherSelData = teacherSelect.getItems();
        List tmp = null;
        studentData.clear();
        teacherData.clear();
        studentSelData.clear();
        teacherSelData.clear();
        tmp = sdao.findAll();
        if (tmp != null && tmp.size() != 0) {
            Object[] arr = tmp.toArray();
            for (Object i : arr) {
                Student v = (Student) i;
                Teacher t = v.getTeacher();
                studentData.add(new StudentModel(v.getId() + "", v.getName(), (t == null ? "null" : t.getId()) + ""));
                teacherSelData.add(v.getName());
            }
        }
        tmp = tdao.findAll();
        if (tmp != null && tmp.size() != 0) {
            Object[] arr = tmp.toArray();
            for (Object i : arr) {
                Teacher v = (Teacher) i;
                teacherData.add(new TeacherModel(v.getId() + "", v.getName()));
                studentSelData.add(v.getName());
            } 
        }
    }
}